package com.example.nacosprovider.util;

import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//package Spring_Test.util;
//
//import org.springframework.http.HttpStatus;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.bind.annotation.ResponseStatus;
//
//

/**
 * 全局异常类
 */
@RestControllerAdvice
public class DuplicateKeyExceptionHaddler {
    @ExceptionHandler({IllegalStateException.class, SQLIntegrityConstraintViolationException.class})
    //设置XMLHttpRequest.status状态码 当前设置为200 Ajax执行success回调函数 若不为200 Ajax执行error回调函数
    @ResponseStatus(value = HttpStatus.OK)
    public String handleTypeMismatchException(SQLIntegrityConstraintViolationException ex) throws Exception {
        System.out.println("用户已存在");
        ex.printStackTrace();
        return JSON.toJSONString(new JsonResult<>("400", "用户名重复"));
    }
}
